const fp = require('lodash/fp')
const cars = [
    {name:'ferrari',horsepower:660,dollar_value:700000,in_stock:true},
    {name:'skyper',horsepower:650,dollar_value:650000,in_stock:false},
    {name:'jaguar',horsepower:550,dollar_value:132000,in_stock:false},
    {name:'adui',horsepower:525,dollar_value:114200,in_stock:false},
    {name:'aston martin',horsepower:750,dollar_value:1850000,in_stock:true},
    {name:'pagani huayr',horsepower:700,dollar_value:1350000,in_stock:false}
]
//练习1 使用fp.flowright重新实现下面这个函数
// let isLastInStock = function(cars) {
//     let last_car = fp.last(cars)
//     return fp.prop('in_stock',last_car)
// }
const isLastInStock = fp.flowRight(fp.prop('in_stock'),fp.last)
console.log(isLastInStock(cars))


//练习2 使用fp.flowRight(),fp.prop(),fp.first()获取第一个car的name
const firstcarname = fp.flowRight(fp.prop('name'),fp.first)
console.log(firstcarname(cars))

//练习3
let _average = function(xs) {
    return fp.reduce(fp.add,0,xs)/xs.length
}
//对下面代码进行改造
// let averageDollarValues = function(cars){
//     let dollar_values = fp.map(function(car){
//         return car.dollar_value
//     },cars)
//     return _average(dollar_values)
// }
let averageDollarValues = function(f,g){
    return function(value){
        return f(g(value))
    }
}
let dollar_values = function(cars){
    return fp.map(car=>car.dollar_value,cars)
}
console.log(averageDollarValues(_average,dollar_values)(cars))

//练习4
let underscore = fp.replace(/\W+/g,'_')
let sanitizeNames = fp.flowRight(fp.toLower,underscore)
console.log(sanitizeNames('Hello World'))